package com.xgkj.diyiketang.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.text.TextUtils;

import com.xgkj.diyiketang.EatShowApp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

public class EatShowSetting {
	private static final String TAG = EatShowSetting.class.getSimpleName();
	//存储文件名
	private static final String SETTING_FILE = "diyiketang_setting";
	//文件存储在sd卡的位置
	public static final String DATA_FOLDER = "diyiketang/data";
	private Context mContext;
	static EatShowSetting setting;
	//缓存用户名
	private static final String LOGIN_NAME = "login_name";
	//缓存密码
	private static final String LOGIN_PSW = "login_psw";
	//缓存昵称
	private  static  final String LOGIN_NICKNAME = "login_nickname";

	private static final String NEW_FRIEND = "new_friend";

	private static final String NEW_COMMENT = "new_comment";

	private static final String NEW_HOME = "new_home";

	private static final String APP_VERSION = "app_version";

	private static final String THIRD_NICKNAME = "third_nickname";

	private static final String THIRD_LOGO = "third_logo";

	private static final String THIRD_OPENID = "third_openid";

	private static final String USERID = "userId";
	private static final String LOACAL = "mlocal";

	private static final String PHOTO_LIST = "photo_list";

	private static final String FIRSTLOCAL = "firstlocal";
	private static final String PHOTO_LIST_SIZE = "photo_list_size";
	private static final String ISBUSINESS = "isBusiness";

	private static final String ISFIRSTSEND ="isfirstsend";
	private static final String THIRDTYPE = "thirdType";

	private EatShowSetting(Context context){
		mContext = context;
	}
	
	public static EatShowSetting getInstance(Context context){
		if(setting == null){
			setting = new EatShowSetting(EatShowApp.getInstance().getApplicationContext());
		}
		return setting;
	}
	
	private SharedPreferences getSharedPreferences(){
		return mContext.getSharedPreferences(SETTING_FILE, Context.MODE_PRIVATE);
	}
	
	//向SharedPreferences中存储String值
	private void setKeyValue(String key, String value){
		SharedPreferences sp = getSharedPreferences();
		SharedPreferences.Editor edit = sp.edit();
		if(value == null){
			edit.remove(key);
		}else{
			edit.putString(key, value);
		}
		edit.commit();
	}
	
	//获取String值
	private String getKeyValue(String key){
		SharedPreferences sp = getSharedPreferences();
		return sp.getString(key, null);
	}
	
	//存储int值
	private void setIntKeyValue(String key, int value){
		SharedPreferences sp = getSharedPreferences();
		SharedPreferences.Editor edit = sp.edit();
		if(key == null){
			edit.remove(key);
		}else{
			edit.putInt(key, value);
		}
		edit.commit();
	}
	
	//获取int值
	private int getIntValue(String key){
		SharedPreferences sp = getSharedPreferences();
		return sp.getInt(key, 0);
	}
	
	//获取带默认值的int值
	private int getIntValue(String key , int def){
		SharedPreferences sp = getSharedPreferences();
		return sp.getInt(key, def);
	}
	
	//存储boolean值
	private void setBooleanKeyValue(String key ,boolean value){
		SharedPreferences sp = getSharedPreferences();
		SharedPreferences.Editor edit = sp.edit();
		edit.putBoolean(key, value);
		edit.commit();
	}
	
	//获取SharedPreferences中存储的boolean值
	private boolean getBooleanValue(String key){
		 SharedPreferences sp = getSharedPreferences();
	        return sp.getBoolean(key, false);
	}
	
	//获取SharedPreferences中带默认值的boolean值
	private boolean getBooleanValue(String key, boolean defvalue) {
        SharedPreferences sp = getSharedPreferences();
        return sp.getBoolean(key, defvalue);
    }

	public void savePhotoList(ArrayList<String> list){
		SharedPreferences sp = getSharedPreferences();
		SharedPreferences.Editor editor = sp.edit();
		editor.putInt(PHOTO_LIST_SIZE,list.size());
		for (int i=0;i<list.size();i++){
			editor.remove(PHOTO_LIST+i);
			editor.putString(PHOTO_LIST+i,list.get(i));
		}
		editor.commit();
	}

	public ArrayList<String> getPhotoList(){
		SharedPreferences sp = getSharedPreferences();
		ArrayList<String> list = new ArrayList<String>();
		list.clear();
		int size = sp.getInt(PHOTO_LIST_SIZE, 0);
		for (int i=0;i<size;i++){
			list.add(getKeyValue(PHOTO_LIST+i));
		}
		return list;
	}
	
	//向文件写入数据
	private void writeData2file(File writefile, String data){
		writefile.delete();
		if(TextUtils.isEmpty(data)){
			return ;
		}
		
		BufferedWriter out = null;
		try {
			OutputStreamWriter fstream = new OutputStreamWriter(
					new FileOutputStream(writefile, true), "UTF-8");
			out = new BufferedWriter(fstream);
			out.write(data);
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(out != null){
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	//从文件中读取数据
	private String readDataFormFile(File readfile){
		BufferedReader in = null;
		String str = null;
		try {
			InputStreamReader istream = new InputStreamReader(new FileInputStream(readfile), "UTF-8");
			in = new BufferedReader(istream);
			str = in.readLine();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(in != null){
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return str;
	}
	
	//获取文件存储路径
	public File getDataFolder(String name) {

        File folder = null;

        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File sdcard = Environment.getExternalStorageDirectory();
            folder = new File(sdcard, name);
            if (!folder.exists()) {
                folder.mkdirs();
            }
        } else {
            folder = mContext.getFilesDir();
            if (!folder.exists()) {
                folder.mkdirs();
            }
        }

        return folder;
    }

	public void setUserId(String userId){
		setKeyValue(USERID, userId);
	}

	public String getUserId(){
		return getKeyValue(USERID);
	}


	public void setFirstLocal(String local){
		setKeyValue(FIRSTLOCAL, local);
	}

	public String getFirstLocal(){
		return getKeyValue(FIRSTLOCAL);
	}


	public void setLocal(String local){
		setKeyValue(LOACAL, local);
	}

	public String getLocal(){
		return getKeyValue(LOACAL);
	}
	
	public void setLoginName(String loginName){
		setKeyValue(LOGIN_NAME, loginName);
	}
	
	public String getLoginName(){
		return getKeyValue(LOGIN_NAME);
	}
	
	public void setLoginPsw(String loginPsw){
		setKeyValue(LOGIN_PSW, loginPsw);
	}
	
	public String getLoginPsw(){
		return getKeyValue(LOGIN_PSW);
	}

	public void setIsBusiness(boolean isbusiness){
		setBooleanKeyValue(ISBUSINESS, isbusiness);
	}

	public boolean getIsBusiness(){
		return getBooleanValue(ISBUSINESS, false);
	}


	public void setIsFirstSend(boolean isfirstsend){
		setBooleanKeyValue(ISFIRSTSEND, isfirstsend);
	}

	public boolean getIsFirstSend(){
		return getBooleanValue(ISFIRSTSEND, true);
	}

	public void setNewFriend(boolean hasnewfriend){
		setBooleanKeyValue(NEW_FRIEND, hasnewfriend);
	}
	
	public boolean getNewFriend(){
		return getBooleanValue(NEW_FRIEND,false);
	}
	
	public void setNewComment(boolean hasnewcomment){
		setBooleanKeyValue(NEW_COMMENT, hasnewcomment);
	}
	
	public boolean getNewComment(){
		return getBooleanValue(NEW_COMMENT,false);
	}
	
	public void setNewHome(boolean hasnewhome){
		setBooleanKeyValue(NEW_HOME, hasnewhome);
	}
	
	public boolean getNewHome(){
		return getBooleanValue(NEW_HOME,false);
	}

	public  String getLoginNickname() {
		return getKeyValue(LOGIN_NICKNAME);
	}

	public void setLoginNickname(String loginNickname){
		setKeyValue(LOGIN_NICKNAME, loginNickname);
	}

	public String getAppVersion(){
		return getKeyValue(APP_VERSION);
	}

	public void setAppVersion(String appVersion){
		setKeyValue(APP_VERSION,appVersion);
	}

	public void setThirdNickname(String nickname){
		setKeyValue(THIRD_NICKNAME,nickname);
	}

	public String getThirdNickname(){
		return getKeyValue(THIRD_NICKNAME);
	}

	public void setThirdLogo(String logo){
		setKeyValue(THIRD_LOGO,logo);
	}

	public String getThirdLogo(){
		return getKeyValue(THIRD_LOGO);
	}

	public void setThirdOpenid(String openid){
		setKeyValue(THIRD_OPENID,openid);
	}

	public String getThirdOpenid(){
		return getKeyValue(THIRD_OPENID);
	}

	public int getThirdType() {
		return getIntValue(THIRDTYPE,-1);
	}

	public void setThirdType(int thirdtype){
		setIntKeyValue(THIRDTYPE,thirdtype);
	}
}
